<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="房间" prop="premisesAddress">
        <el-input v-model="queryParams.premisesAddress" clearable placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="payState">
        <el-select v-model="queryParams.payState" placeholder="请选择" clearable>
          <el-option :label="dict.label" :value="dict.value" v-for="dict in dict.type.pay_state.slice(0,3)" :key="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>
    <div class="mb8">
      <el-row>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

    </div>
    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
      ref="table"
    >
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="房间" prop="premisesAddress"/>
      <el-table-column label="业主名称" prop="userName"/>
      <el-table-column label="账单类型" prop="billType">
        <template #default="scope">{{ billTypeText(scope.row.billType) }}</template>
      </el-table-column>
      <el-table-column label="金额（元）" prop="cost"/>
      <el-table-column label="账单生成日期" prop="createTime">
        <template #default="scope">{{ $moment(scope.row.createTime).format('YYYY/MM/DD') }}</template>
      </el-table-column>
      <el-table-column label="最晚付款日期" prop="latestPayTime">
        <template #default="scope">{{ $moment(scope.row.latestPayTime).format('YYYY/MM/DD') }}</template>
      </el-table-column>
      <el-table-column label="状态" prop="payState">
        <template #default="scope">
          <dict-tag :options="dict.type.pay_state" :value="scope.row.payState"/>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['bill:proprietor:query']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOfflinePayment(scope.row)"
            v-if="scope.row.payState == 1 || scope.row.payState == 3"
            v-hasPermi="['bill:proprietor:offlinePayment']"
          >线下处理
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--  详情  -->
    <Detail v-model="detailDialogVisible" :title="title" :data="form"/>
    <!--  线下支付  -->
    <OfflinePayment v-model="offlinePaymentDialogVisible" :data="form" @success="getList"/>
  </div>
</template>

<script>
import Detail from './components/detail.vue'
import OfflinePayment from './components/offline-payment.vue'
import { getProprietorBill, listProprietorBill } from '@/api/bill/proprietor'

export default {
  name: 'lesseeSource',
  dicts: ['pay_state'],
  components: {
    Detail,
    OfflinePayment
  },
  data() {
    return {
      showSearch: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 角色表格数据
      list: [],
      // 弹出层标题
      title: '',
      //表格行id
      id: undefined,
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 是否显示详情弹窗
      detailDialogVisible: false,
      // 是否显示线下支付弹窗
      offlinePaymentDialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        premisesAddress: '', //房间
        payState: '' //状态
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  computed: {
    //账单类型文本
    billTypeText() {
      return (value) => {
        let obj = [{ value: 1, label: '一次性服务费' }].find(item => item.value == value)
        return obj ? obj.label : ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listProprietorBill(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.list = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.contractId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      getProprietorBill(row.proprietorBillId).then((response) => {
        this.form = { ...response.data }
        this.title = '账单详情'
        this.detailDialogVisible = true
      })
    },
    /** 线下支付按钮操作 */
    handleOfflinePayment(row) {
      this.form = { ...row }
      this.offlinePaymentDialogVisible = true
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
